import 'element-plus/theme-chalk/index.css'
import Validator from '@/utils/Validator'
let loadingInstance;
// 消息提示
function msg(content) {
  ElMessage.info(content)
}
// 错误消息
function msgError(content) {
  ElMessage.error(content)
}
// 成功消息
function msgSuccess(content) {
  ElMessage.success(content)
}
// 警告消息
function msgWarning(content) {
  ElMessage.warning(content)
}
// 弹出提示
function alert(content) {
  ElMessageBox.alert(content, "系统提示")
}
// 错误提示
function alertError(content) {
  ElMessageBox.alert(content, "系统提示", { type: 'error' })
}
// 成功提示
function alertSuccess(content) {
  ElMessageBox.alert(content, "系统提示", { type: 'success' })
}
// 警告提示
function alertWarning(content) {
  ElMessageBox.alert(content, "系统提示", { type: 'warning' })
}
// 通知提示
function notify(content) {
  ElNotification.info(content)
}
// 错误通知
function notifyError(content) {
  ElNotification.error(content);
}
// 成功通知
function notifySuccess(content) {
  ElNotification.success(content)
}
// 警告通知
function notifyWarning(content) {
  ElNotification.warning(content)
}
// 确认窗体
function confirm(content) {
  return ElMessageBox.confirm(content, "系统提示", {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: "warning",
  })
}
// 提交内容
function prompt(content) {
  return ElMessageBox.prompt(content, "系统提示", {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: "warning",
  })
}
// 打开遮罩层
function loading(content) {
  loadingInstance = ElLoading.service({
    lock: true,
    text: content,
    background: "rgba(0, 0, 0, 0.7)",
  })
}
// 关闭遮罩层
function closeLoading() {
  loadingInstance.close();
}

/**
 * uniapp 类似构建
 * @param {*} obj 
 */
function showToast(obj){
  let title = obj?.title
  if(Validator.isEmpty(title)) title = '默认提示'
  let icon = obj?.icon
  switch(icon){
    case 'success':{
      msgSuccess(title)
      break
    }
    case 'error':{
      msgError(title)
      break
    }
    case 'warn':{
      msgWarning(title)
      break
    }
    case 'warning':{
      msgWarning(title)
      break
    }
    case 'info':{
      msg(title)
      break
    }
    default:{
      msg(title)
    }
  }
}

function showModal(obj){
  let title = obj?.title
  if(Validator.isEmpty(title)) title = '默认提示'
  let content = obj?.content
  if(Validator.isEmpty(content)) content = ''
  let config = {
    confirmButtonText: '确认'
  }
  if(!Validator.isEmpty(obj?.confirmButtonText)){
    config.confirmButtonText = obj?.confirmButtonText
  }
  config.cancelButtonText = obj?.cancelText || '取消'
  if(!Validator.isEmpty(obj?.type)){
    config.type = obj?.type
  }
  if(obj?.showCancel == false){
    return new Promise(function(resolve,reject){
      ElMessageBox.alert(
        content,
        title,
        config
      )
        .then(() => {
          let res = {
            confirm:true,
            cancel:false
          }
          resolve(res)
        })
        .catch(() => {
          let res = {
            confirm:false,
            cancel:true
          }
          resolve(res)
        })
    })
  }else{
    return new Promise(function(resolve,reject){
      ElMessageBox.confirm(
        content,
        title,
        config
      )
        .then(() => {
          let res = {
            confirm:true,
            cancel:false
          }
          resolve(res)
        })
        .catch(() => {
          let res = {
            confirm:false,
            cancel:true
          }
          resolve(res)
        })
    })
  }
  
}

export default {
  msg,
  msgSuccess,
  msgError,
  msgWarning,
  alert,
  alertError,
  alertSuccess,
  alertWarning,
  notify,
  notifyError,
  notifySuccess,
  notifyWarning,
  confirm,
  prompt,
  loading,
  closeLoading,
  showToast,
  showModal
}
